from django.shortcuts import render, HttpResponse
from django.views import View
import random  # 随机数模块
from utils.tencent.sms import send_sms_single  # 调用短信模块
from django.conf import settings
from .form import RegisterModelFOrm


# Create your views here.

class Test(View):
    """测试"""

    def get(self, request):
        tpl = request.GET.get('tpl')

        template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)
        print(tpl, template_id)
        if not template_id:
            return HttpResponse('参数错误')

        code = random.randrange(1000, 9999)
        res = send_sms_single('17683734889', template_id, [code, ])  # 第一个参数是电话号码,第二个参数模块码,第三个是随机生成的一个验证码
        # res 返回 {'result':0,'errmsg':'ok','ext':''}
        if res['result'] == 0:
            return HttpResponse("发送成功")
        return HttpResponse('失败', )


class RegisterView(View):
    def get(self, request):
        form = RegisterModelFOrm()  # 实例化form
        return render(request, 'app01/register.html', {'form': form})
